Repository navigation
fix: run signal effects inline only when session is locked - #27188
totally-not-ai[bot] wants to merge 2 commits into
Conversation
An effect ran inline whenever its UI was the current UI, also after the request had released the session lock, e.g. in a request end listener, and then failed on the lock check. Fixes #27187
Type of change
|
|
|
Closing in favour of fixing the root cause instead of adding a lock check to effects: a current UI must mean its session is locked by this thread (see the rules in #27198). #27201 restores the current instances when request handlers release the lock, and #27439 fires the session lock released event without a current UI. Together they cover #27187. |
|
@totally-not-ai Would it make sense to still apply this as a precaution and to avoid signals to work incorrectly even if the environment is incorrectly set up? |
|
@Artur- Yes, it is worth keeping as a precaution. It is cheap and only kicks in when the rule "current UI means locked session" is already broken.
|
…CP: 25.4) Related to #27187, #27188 **Hardening** · flow-server · signal effects owned by a UI Signal effects, the signal result notifier and the background change flag treated "this UI is current" as "this thread holds the session lock". Flow's request handling now keeps that rule (#27201, #27439). Code outside Flow can still break it, for example by calling `UI.setCurrent` without locking the session. Then an effect touched UI state without the lock and failed with "Cannot access state in VaadinSession or UI without locking the session". After that, its error handler failed the same way. **Risks:** -⚠️ Behaviour change, only when the UI is current but its session is not locked by this thread: the effect, or the result notifier task, is queued through `UI.access` instead of running right away. `isBackgroundChange()` reports `true` for that run. - When the UI is current and locked, which is every normal case, nothing changes. - ✅ No new public API. The shared check is `UIInternals.isCurrentAndLocked()`, which is in the internal package. The added cost is one `hasLock()` call. - `ElementEffect`, the result notifier in `VaadinService` and `Effect`'s background change flag use the new check. - `guidelines/architecture.md` now describes signal effects as checking the session lock instead of trusting the current UI. - Three existing `EffectTest` owner-UI tests now use a plain `UI` with a mocked session that reports the lock as held, instead of a Mockito `UI` mock. The owner UI's internals and session lock are now read, and a Mockito `UI` has no internals. They avoid `MockUI` because it starts a `VaadinService`, which registers a signal environment that stays active for the rest of the test class. - Tests: an effect and a result notifier task triggered while the owner UI is current but unlocked run later with the lock held, and the effect reports a background change. Both tests fail without the fix. Working on behalf of @Artur- --------- Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com> Co-authored-by: caalador <mikael.grankvist@vaadin.com>
) Related to vaadin#27187, vaadin#27188 **Hardening** · flow-server · signal effects owned by a UI Signal effects, the signal result notifier and the background change flag treated "this UI is current" as "this thread holds the session lock". Flow's request handling now keeps that rule (vaadin#27201, vaadin#27439). Code outside Flow can still break it, for example by calling `UI.setCurrent` without locking the session. Then an effect touched UI state without the lock and failed with "Cannot access state in VaadinSession or UI without locking the session". After that, its error handler failed the same way. **Risks:** -⚠️ Behaviour change, only when the UI is current but its session is not locked by this thread: the effect, or the result notifier task, is queued through `UI.access` instead of running right away. `isBackgroundChange()` reports `true` for that run. - When the UI is current and locked, which is every normal case, nothing changes. - ✅ No new public API. The shared check is `UIInternals.isCurrentAndLocked()`, which is in the internal package. The added cost is one `hasLock()` call. - `ElementEffect`, the result notifier in `VaadinService` and `Effect`'s background change flag use the new check. - `guidelines/architecture.md` now describes signal effects as checking the session lock instead of trusting the current UI. - Three existing `EffectTest` owner-UI tests now use a plain `UI` with a mocked session that reports the lock as held, instead of a Mockito `UI` mock. The owner UI's internals and session lock are now read, and a Mockito `UI` has no internals. They avoid `MockUI` because it starts a `VaadinService`, which registers a signal environment that stays active for the rest of the test class. - Tests: an effect and a result notifier task triggered while the owner UI is current but unlocked run later with the lock held, and the effect reports a background change. Both tests fail without the fix. Working on behalf of @Artur- --------- Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com> Co-authored-by: caalador <mikael.grankvist@vaadin.com>



Fixes #27187
behavior deviation · flow-server · apps that change a signal after the request has released the session lock, e.g. in a request end listener
An element effect or a signal result callback ran right away whenever its UI was the current UI. After a request ends, the session lock is already released but the request's UI is still current, so the code ran without the lock and failed with "Cannot access state in VaadinSession or UI without locking the session". Such runs now go through
UI.accesslike changes from any other thread. Workaround before this fix: clear the current instances around the signal write.Risks:
isBackgroundChange() == true, consistent with it now being dispatched like a background change.UIInternals.isCurrentAndLocked()in the internalcomponent.internalpackage so the three places share one check.UI.access; code that holds the lock is unchanged.Context. The inline path exists so that changes made during a request apply in the same round trip. It only checked the current UI, which was enough before request end listeners and interceptors started running code after the lock is released.
UIInternals.isCurrentAndLocked(): the UI is current and its session is locked by the current thread.ElementEffectruns an effect inline only when that holds, both in the dispatcher and in its test-environment fallback; otherwise it usesui.access.VaadinServiceuses the same check.Effectuses the same check to decide whether a change is a background change.EffectTestbackground-change tests now use a real UI instead of a mock.